package com.yaopaikeji.dao;

import com.alibaba.fastjson.JSONObject;
import com.yaopaikeji.constants.Constants;
import com.yaopaikeji.exception.DeleteException;
import com.yaopaikeji.exception.InsertException;
import com.yaopaikeji.exception.InvalidNullException;
import com.yaopaikeji.mapper.UserGroupRelationMapper;
import com.yaopaikeji.model.EmailGroupInfo;
import com.yaopaikeji.model.User;
import com.yaopaikeji.model.UserGroupRelation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserGroupRelationDao extends BaseDao<UserGroupRelation> {

    private static final Logger logger = LoggerFactory.getLogger(UserGroupRelationDao.class);

    @Autowired
    private UserGroupRelationMapper userGroupRelationMapper;

    @Override
    public UserGroupRelation findByPrimaryKey(int id) {
        return userGroupRelationMapper.findRelationByPrimarykey(id);
    }

    @Override
    public List<UserGroupRelation> findAll() {
        return userGroupRelationMapper.findRealationsAll();
    }

    public List<User> findUsersByGroupId(int groupId){

        return userGroupRelationMapper.findUsersByGroupId(groupId);
    }

    public List<EmailGroupInfo> findGroupsByUserId(int userId){

        return userGroupRelationMapper.findEmailGroups(userId);
    }

    public boolean addNewRelation(UserGroupRelation userGroupRelation) {

        int rows = userGroupRelationMapper.insertRelation(userGroupRelation);

        if (rows < 1) {

            logger.error("增加用户到邮件组出错，data=" + JSONObject.toJSONString(userGroupRelation));
            throw new InsertException(String.format(Constants.INSERT_ERROR_FMT, "用户到邮件组关系"));
        }

        return true;
    }

    public boolean addNewRelations(List<UserGroupRelation> userGroupRelations) {

        if (userGroupRelations == null) {

            logger.error("批量增加用户到邮件组出错,参数为空");
            throw new InvalidNullException();
        }
        int rows = userGroupRelationMapper.insertRelations(userGroupRelations);

        if (rows != userGroupRelations.size()) {

            logger.error("批量插入用户到邮件组出错,插入成功的数量少于请求数量,data=" + JSONObject.toJSONString(userGroupRelations));
            throw new InsertException(String.format(Constants.INSERT_ERROR_FMT, "批量用户到邮件组"));
        }

        return true;
    }

    public boolean deleteRelationByPrimaryKey(int id) {

        int rows = userGroupRelationMapper.deleteRelationByPrimaryKey(id);

        if (rows < 1) {

            logger.error("删除用户和邮件组出错，id=" + id);
            throw new DeleteException(String.format(Constants.DELETE_ERROR_FMT, "用户和邮件组关系"));
        }

        return true;
    }

    public boolean deleteRelation(UserGroupRelation userGroupRelation) {

        int rows = userGroupRelationMapper.deleteRelation(userGroupRelation.getUser().getId(), userGroupRelation.getEmailGroupInfo().getId());
        if (rows < 1) {

            logger.error("解除用户和邮件组关系出错，data=" + JSONObject.toJSONString(userGroupRelation));
            throw new DeleteException(String.format(Constants.DELETE_ERROR_FMT, "用户和用户组关系"));
        }

        return true;
    }

    public boolean deleteGroupMembers(int groupId){

        //某个邮件组可能没有用户，这里不做影响行数判断
        userGroupRelationMapper.deleteEmailGroupRelation(groupId);
        return true;
    }

    public boolean deleteRelationByUserId(int userId){

        //某个用户可能没有加入任何邮件组，这里不做影响行数判断
        userGroupRelationMapper.deleteRelationByUserId(userId);
        return true;
    }
}
